package com.itstherules.jmsunit.queuemanager.activemq;

import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;

import com.itstherules.jmsunit.queuemanager.BaseQueueManager;

public final class ActiveMQQueueManager extends BaseQueueManager {

	public ActiveMQQueueManager(String uri) throws Exception {
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, uri);
		connection = (ActiveMQConnection) factory.createConnection();
		((ActiveMQConnection) connection).setSendTimeout(2000);
		connection.start();
		session = (ActiveMQSession) connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
	}
	
	public final MessageConsumer getMessageConsumerWithMessageID(String queue, String messageId) throws JMSException {
		String selector = "JMSMessageID = '" + messageId + "'";
		return session.createConsumer(this.createQueue(queue), selector);
	}

}
